
import tkinter as tk  
from tkinter import ttk
from tkinter import scrolledtext

from scp import SCPClient 
import os
import stat
import movefile as mvf
import confile_deal as configfile
width=800 
height=300
configfile_path="./toolconfig.json"


source_ip = "192.168.11.172"
source_port = 22
source_user = "hujingxuan"
source_password = "hujingxuan"
source_file ="/home/hujingxuan/tmp/tmp.V0gwzMhox1/cmake-build-release"
filename = "eventTriggeredSrv"


target_ip = "192.168.28.234"
target_port = 22
target_user = "gold"
target_password = "gold"
target_file ="/app/aarch64/lems/service/eventTriggeredSrv"





def hit_me():
    print("原始位置"+dataLabel2.get())
    # tshow.insert("end","12314654\n")
    tshow.insert("end","开始执行\n")
    tshow.see("end")
    tshow.update()

    source_ip=dataLabel_ip.get()
    source_user=dataLabel_usr.get()
    source_password=dataLabel_pswd.get()
    source_file=dataLabel2.get()
    filename=dataLabel3.get()

    target_ip=dataLabel_ip_t.get()
    target_user=dataLabel_usr_t.get()
    target_password=dataLabel_pswd_t.get()
    target_file=dataLabel4.get()


    tshow.insert("end",f"source_ip={source_ip}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"source_port={source_port}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"source_user={source_user}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"source_password={source_password}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"source_file={source_file}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"filename={filename}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"target_ip={target_ip}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"target_port={target_port}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"target_user={target_user}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"target_password={target_password}\n")
    tshow.see("end")
    tshow.update()
    tshow.insert("end",f"target_file={target_file}\n")
    tshow.see("end")
    tshow.update()

    outfolder = "./mid"
    print("开始执行")

    os.makedirs(outfolder,exist_ok=True)  
    source_file 
    # scp_downloads(target_ip, target_port, target_user, target_password, target_file+"/config", outfolder+"/config")
    mvf.remote_cmd(target_ip,target_port,target_user,target_password,f"sudo chmod 777 {target_file}")

    try:  
        mvf.scp_download(source_ip, source_port,source_user,source_password,source_file+"/"+filename, outfolder)
        print(f"文件下载成功。{source_file}/{filename}")  
        tshow.insert("end",f"文件下载成功。{source_file}/{filename}\n")
        tshow.see("end")
        tshow.update()
    except Exception as e:  
        print(f"文件下载发生错误：{e}")
        tshow.insert("end",f"文件下载发生错误：{e}\n")
        tshow.see("end")
        tshow.update()
    mvf.remote_cmd(target_ip,target_port,target_user,target_password,f"sudo su")
    print(f"删除原始 {target_file}/{filename} 文件")
    tshow.insert("end",f"删除原始 {target_file}/{filename} 文件\n")
    tshow.see("end")
    tshow.update()
    mvf.remote_cmd(target_ip,target_port,target_user,target_password,f"rm {target_file}/{filename}")

    try:  
        mvf.scp_put(target_ip, target_port,target_user,target_password ,target_file,outfolder+"/"+filename)
        print(f"文件上传成功  {target_file}/{filename}")  
        tshow.insert("end",f"文件上传成功  {target_file}/{filename}\n")
        tshow.see("end")
        tshow.update()
    except Exception as e:  
        print(f"文件上传发生错误：{e}")
        tshow.insert("end",f"文件上传发生错误：{e}\n")
        tshow.see("end")
        tshow.update()
    pass
    mvf.remote_cmd(target_ip,target_port,target_user,target_password,f"chmod 775 {target_file}/{filename}")
    print(f"chmod 775 {target_file}/{filename}")
    tshow.insert("end",f"chmod 775 {target_file}/{filename}\n")

    tshow.insert("end",f"上传完成\n")
    tshow.see("end")
    tshow.update()


def save_param():
    print("save_param")
    source_ip=dataLabel_ip.get()
    source_user=dataLabel_usr.get()
    source_password=dataLabel_pswd.get()
    source_file=dataLabel2.get()
    filename=dataLabel3.get()

    target_ip=dataLabel_ip_t.get()
    target_user=dataLabel_usr_t.get()
    target_password=dataLabel_pswd_t.get()
    target_file=dataLabel4.get()
    configfile.WriteConfigData(configfile_path, cobox1.get(),source_ip,source_port,source_user,source_password,source_file,filename,target_ip,target_port,target_user,target_password,target_file)

def Init_configParam():
    print("Init_configParam")
    source_ip,source_port,source_user,source_password,source_file,filename,target_ip,target_port,target_user,target_password,target_file=configfile.ReadConfigData(configfile_path,cobox1.get())
    dataLabel2.delete(0, "end")
    dataLabel2.insert(0, source_file)  
    dataLabel3.delete(0, "end")
    dataLabel3.insert(0, filename)  
    dataLabel_ip.delete(0, "end")
    dataLabel_ip.insert(0, source_ip)  
    dataLabel_usr.delete(0, "end")
    dataLabel_usr.insert(0, source_user)  
    dataLabel_pswd.delete(0, "end")
    dataLabel_pswd.insert(0, source_password)  


    dataLabel4.delete(0, "end")
    dataLabel4.insert(0, target_file)  
    # dataLabel5.delete(0, "end")
    # dataLabel5.insert(0, "")  
    dataLabel_ip_t.delete(0, "end")
    dataLabel_ip_t.insert(0, target_ip)  
    dataLabel_usr_t.delete(0, "end")
    dataLabel_usr_t.insert(0, target_user)  
    dataLabel_pswd_t.delete(0, "end")
    dataLabel_pswd_t.insert(0, target_password)  


def cobox1change(event):
    source_ip,source_port,source_user,source_password,source_file,filename,target_ip,target_port,target_user,target_password,target_file=configfile.ReadConfigData(configfile_path, cobox1.get())
    dataLabel2.delete(0, "end")
    dataLabel2.insert(0, source_file)  
    dataLabel3.delete(0, "end")
    dataLabel3.insert(0, filename)  
    dataLabel_ip.delete(0, "end")
    dataLabel_ip.insert(0, source_ip)  
    dataLabel_usr.delete(0, "end")
    dataLabel_usr.insert(0, source_user)  
    dataLabel_pswd.delete(0, "end")
    dataLabel_pswd.insert(0, source_password)  


    dataLabel4.delete(0, "end")
    dataLabel4.insert(0, target_file)  
    # dataLabel5.delete(0, "end")
    # dataLabel5.insert(0, "")  
    dataLabel_ip_t.delete(0, "end")
    dataLabel_ip_t.insert(0, target_ip)  
    dataLabel_usr_t.delete(0, "end")
    dataLabel_usr_t.insert(0, target_user)  
    dataLabel_pswd_t.delete(0, "end")
    dataLabel_pswd_t.insert(0, target_password)  

def main():  
    jianju=25
    liftjianju=8
    window = tk.Tk()
    window.title('My tool')
    size = str(width)+"x"+str(height)
    window.geometry(size)  

    menubar = tk.Menu(window)
    filemenu = tk.Menu(menubar, tearoff=0)
    menubar.add_cascade(label='选项', menu=filemenu)
    
    submenu = tk.Menu(filemenu) 
    filemenu.add_cascade(label='功能', menu=submenu,underline=0)
    submenu.add_command(label='一键部署', command=hit_me)  

    filemenu.add_command(label='测试', command=hit_me)
    global cobox1
    global dataLabel2 #原始位置
    global dataLabel3
    global dataLabel4
    # global dataLabel5
    global dataLabel6
    global dataLabel_ip
    global dataLabel_usr
    global dataLabel_pswd
    global dataLabel_ip_t
    global dataLabel_usr_t
    global dataLabel_pswd_t
    global tshow
    # global text_area

    tk.Label(window, text='默认配置', font=('Arial', 10)).place(x=liftjianju,y=0) 
    tk.Label(window, text='原始位置', font=('Arial', 10)).place(x=liftjianju,y=jianju*1) 
    tk.Label(window, text='ip', font=('Arial', 10)).place(x=liftjianju+380,y=jianju*1) 
    tk.Label(window, text='user', font=('Arial', 10)).place(x=liftjianju+520,y=jianju*1) 
    tk.Label(window, text='pswd', font=('Arial', 10)).place(x=liftjianju+630,y=jianju*1) 
    tk.Label(window, text='目标文件', font=('Arial', 10)).place(x=liftjianju,y=jianju*2) 
    tk.Label(window, text='目标位置', font=('Arial', 10)).place(x=liftjianju,y=jianju*3) 
    tk.Label(window, text='ip', font=('Arial', 10)).place(x=liftjianju+380,y=jianju*3) 
    tk.Label(window, text='user', font=('Arial', 10)).place(x=liftjianju+520,y=jianju*3) 
    tk.Label(window, text='pswd', font=('Arial', 10)).place(x=liftjianju+630,y=jianju*3) 
    # tk.Label(window, text='路径', font=('Arial', 10)).place(x=liftjianju,y=jianju*4) 
    tk.Label(window, text='结束选项', font=('Arial', 10)).place(x=liftjianju,y=jianju*5) 

   
    # scrollbar= tk.Scrollbar(window,orient='vertical')
    # scrollbar.pack(side=tk.RIGHT,fill='y')
    tshow = tk.Text(window,height=8,width=80)


    sl = tk.Scrollbar(window)
    sl.pack(side = tk.RIGHT,fill = tk.Y)

    text_area = scrolledtext.ScrolledText(window, width=50, height=8, wrap=tk.WORD)


    cobox1 = ttk.Combobox(window)

    configlist=configfile.ReadconfigList(configfile_path)
    cobox1["value"] = configlist   # #给下拉菜单设定值
    cobox1.current(0)
    cobox1.bind('<<ComboboxSelected>>', cobox1change)
    # lbox1.insert(2, 'dasd')   
    cobox2 = ttk.Combobox(window)
    # dataLabel1 = tk.Entry(window, show=None, font=('Arial', 10),width=50)  
    dataLabel2 = tk.Entry(window, show=None, font=('Arial', 10),width=40)  
    dataLabel3 = tk.Entry(window, show=None, font=('Arial', 10),width=40) 
    dataLabel_ip = tk.Entry(window, show=None, font=('Arial', 10),width=15) 
    dataLabel_usr = tk.Entry(window, show=None, font=('Arial', 10),width=10) 
    dataLabel_pswd = tk.Entry(window, show=None, font=('Arial', 10),width=10) 
    dataLabel_ip_t = tk.Entry(window, show=None, font=('Arial', 10),width=15) 
    dataLabel_usr_t = tk.Entry(window, show=None, font=('Arial', 10),width=10) 
    dataLabel_pswd_t = tk.Entry(window, show=None, font=('Arial', 10),width=10) 
    dataLabel4 = tk.Entry(window, show=None, font=('Arial', 10),width=40)  
    # dataLabel5 = tk.Entry(window, show=None, font=('Arial', 10),width=40)  
    # dataLabel6 = tk.Entry(window, show=None, font=('Arial', 10),width=50)  

    cobox1.place(x=80+liftjianju,y=0)
    dataLabel2.place(x=80+liftjianju,y=jianju*1)
    dataLabel_ip.place(x=80+liftjianju+320,y=jianju*1)
    dataLabel_usr.place(x=80+liftjianju+320+150,y=jianju*1)
    dataLabel_pswd.place(x=80+liftjianju+320+270,y=jianju*1)
    dataLabel3.place(x=80+liftjianju,y=jianju*2)
    dataLabel4.place(x=80+liftjianju,y=jianju*3)
    dataLabel_ip_t.place(x=80+liftjianju+320,y=jianju*3)
    dataLabel_usr_t.place(x=80+liftjianju+320+150,y=jianju*3)
    dataLabel_pswd_t.place(x=80+liftjianju+320+270,y=jianju*3)
    # dataLabel5.place(x=80+liftjianju,y=jianju*4)
    cobox2.place(x=80+liftjianju,y=jianju*5)
  
    tshow.place(x=100,y=160)

    # tshow.pack()
    # scrollbar.config(command=tshow.yview)

    tshow['yscrollcommand'] = sl.set
    sl['command'] = tshow.yview


    tk.Button(window, text='开始执行', font=('Arial', 10), width=8, height=1, command=hit_me).place(x=10,y=160)
    tk.Button(window, text='保存参数', font=('Arial', 10), width=8, height=1, command=save_param).place(x=10,y=190)
    Init_configParam()
    window.config(menu=menubar)
 

    window.mainloop()









if __name__ == "__main__":  
    main()
    # pip install xxx-package -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com